#region License Information
// Copyright (c) 2005 - 2009, Allan Spartacus Mangune (allan@owlpal.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list 
//   of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of 
//   conditions and the following disclaimer in the documentation and/or other materials provided 
//   with the distribution.
//
// * Neither the name of Owlpal nor the names of its contributors may be used to endorse or 
//   promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
// FITNESS FOR AllowHtmlInComment PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Owlpal.Models.Repositories;

namespace Owlpal.Models
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Blog
    {
        #region Members
        Guid id;
        private string name;
        private string description;
        private Int64 entryCount = 0;
        private string title;
        private bool isPrivate;
        private string theme;
        private bool enableFeeder;
        private bool enbablePublishing;
        private bool enableComment;
        private bool enableTrackback;
        private bool enablePing;
        private bool enableEmailReferral;
        private bool enableRichEditor;
        private bool enablePublishing;
        private bool enbleAnonymousCommentModeration;
        private bool enableNewCommentNotification;
        private bool allowHtmlInComment;
        private bool allowAnonymousComment;
        private string emailAddressForCommentNotification;
        private string emailAddressForEntryCreation;
        private short entryDisplayCount;
        private string entryDisplayType;
        private string entryDisplaySortOrder;
        private string dateLocale;
        private short linkCount;
        private short feedCount;
        private string copyrightPeriod;
        private string copyrightOwner;
        private string timezone;
        private bool isDeleted;
        private bool isPrimary;
        private string defaultEntryStatus;
        private DateTime creationDate;
        private DateTime lastUpdateDate;
        private MembershipUser creator;
        private MembershipUser updator;
        private List<Category> categories;
        private List<Entry> entries;
        private MembershipUserCollection authors;
        private short linkQuantity;
        private short feedQuantity;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the unique ID of the Blog.
        /// </summary>
        public Guid ID
        {
            get { return this.id; }
            set { this.id = value; }
        }

        /// <summary>
        /// Gets or sets the unique name of the Blog.
        /// </summary>
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        /// <summary>
        /// Gets or sets the Blog's description.
        /// </summary>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        /// <summary>
        /// Gets or sets the Categories that belong to this Blog.
        /// </summary>
        public List<Category> Categories
        {
            get { return this.categories; }
            set { this.categories = value; }
        }

        /// <summary>
        /// Gets or sets the Entries of this Blog.
        /// </summary>
        public List<Entry> Entries
        {
            get { return this.entries; }
            set { this.entries = value; }
        }
        
        /// <summary>
        /// Gets the total number of entries on this Blog.
        /// </summary>
        public Int64 EntryCount
        {
            get { return this.entryCount; }
        }

        /// <summary>
        /// Gets or sets the Authors of this Blog.
        /// </summary>
        public MembershipUserCollection Authors
        {
            get { return this.authors; }
            set { this.authors = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the Trackback
        /// is enabled.
        /// </summary>
        public bool EnableTrackback
        {
            get { return this.enableTrackback; }
            set { this.enableTrackback = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the publishing of 
        /// Blog entries is allowed.
        /// </summary>
        public bool EnablePublishing
        {
            get { return this.enablePublishing; }
            set { this.enablePublishing = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not anonymous 
        /// comments are allowed for moderation.
        /// </summary>
        public bool EnbleAnonymousCommentModeration
        {
            get { return this.enbleAnonymousCommentModeration; }
            set { this.enbleAnonymousCommentModeration = value; }
        }

        /// <summary>
        /// Gets or sets the number of entries to be displayed in a Page.
        /// </summary>
        public short EntryDisplayCount
        {
            get { return this.entryDisplayCount; }
            set { this.entryDisplayCount = value; }
        }

        /// <summary>
        /// Gets or sets the sort order of Blog's entries.
        /// </summary>
        public string EntryDisplaySortOrder
        {
            get { return this.entryDisplaySortOrder; }
            set { this.entryDisplaySortOrder = value; }
        }

        /// <summary>
        /// Gets or sets the display type of the Blog's entries.
        /// </summary>
        public string EntryDisplayType
        {
            get { return this.entryDisplayType; }
            set { this.entryDisplayType = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not this Blog
        /// has been deleted and therefore is no longer accessible.
        /// </summary>
        public bool IsDeleted
        {
            get { return this.isDeleted; }
            set { this.isDeleted = value; }
        }

        /// <summary>
        /// Gets or sets a value indication whether or not this is the main Blog.
        /// </summary>
        public bool IsPrimary
        {
            get { return this.isPrimary; }
            set { this.isPrimary = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not this Blog
        /// is viewable by unauthenticated users.
        /// </summary>
        public bool IsPrivate
        {
            get { return this.isPrivate; }
            set { this.isPrivate = value; }
        }

        /// <summary>
        /// Gets or sets the date in which this Blog was last updated.
        /// </summary>
        public DateTime LastUpdateDate
        {
            get { return this.lastUpdateDate; }
            set { this.lastUpdateDate = value; }
        }

        /// <summary>
        /// Gets or sets the Theme that will be used by this Blog.
        /// </summary>
        public string Theme
        {
            get { return this.theme; }
            set { this.theme = value; }
        }

        /// <summary>
        /// Gets or sets the Timezone of the Blog user.
        /// </summary>
        public string Timezone
        {
            get { return this.timezone; }
            set { this.timezone = value; }
        }

        /// <summary>
        /// Gets or sets the title of this Blog.
        /// </summary>
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not anonymous users are allowed to post comments.
        /// </summary>
        public bool AllowAnonymousComment
        {
            get { return this.allowAnonymousComment; }
            set { this.allowAnonymousComment = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not HTML tags are
        /// allowed in comment.
        /// </summary>
        public bool AllowHtmlInComment
        {
            get { return this.allowHtmlInComment; }
            set { this.allowHtmlInComment = value; }
        }

        /// <summary>
        /// Gets or sets the copyright owner of the Blog.
        /// </summary>
        public string CopyrightOwner
        {
            get { return this.copyrightOwner; }
            set { this.copyrightOwner = value; }
        }

        /// <summary>
        /// Gets or sets the period covered in copyright.
        /// </summary>
        public string CopyrightPeriod
        {
            get { return this.copyrightPeriod; }
            set { this.copyrightPeriod = value; }
        }

        /// <summary>
        /// Gets of sets the User who created this Blog.
        /// </summary>
        public MembershipUser Creator
        {
            get { return this.creator; }
            set { this.creator = value; }
        }

        /// <summary>
        /// Gets or sets the User who last updated this Blog.
        /// </summary>
        public MembershipUser Updator 
        {
            get { return this.updator; }
            set { this.updator = value; }
        }

        /// <summary>
        /// Gets or sets the date in which this Blog was created.
        /// </summary>
        public DateTime CreationDate
        {
            get { return this.creationDate; }
            set { this.creationDate = value; }
        }

        /// <summary>
        /// Gets or sets the local date setting of the Blog users.
        /// </summary>
        public string DateLocale
        {
            get { return this.dateLocale; }
            set { this.dateLocale = value; }
        }

        /// <summary>
        /// Gets or sets the email address to which a message is sent 
        /// regarding a newly created Blog.
        /// </summary>
        public string EmailAddressForEntryCreation
        {
            get { return this.emailAddressForEntryCreation; }
            set { this.emailAddressForEntryCreation = value; }
        }

        /// <summary>
        /// Gets or sets the email address to which a message is sent 
        /// regarding a newly posted comment to the Blog's entries.
        /// </summary>
        public string EmailAddressForCommentNotification
        {
            get { return this.emailAddressForCommentNotification; }
            set { this.emailAddressForCommentNotification = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the Blog's
        /// readers can post comments to entries.
        /// </summary>
        public bool EnableComment
        {
            get { return this.enableComment; }
            set { this.enableComment = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not Feed is
        /// enabled.
        /// </summary>
        public bool EnableFeeder
        {
            get { return this.enableFeeder; }
            set { this.enableFeeder = value; }
        }

        /// <summary>
        /// Gets or sets a value indicationg whether or not a
        /// notification is sent when a new comment was posted.
        /// </summary>
        public bool EnableNewCommentNotification
        {
            get { return this.enableNewCommentNotification; }
            set { this.enableNewCommentNotification = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not Ping is enabled.
        /// </summary>
        public bool EnablePing
        {
            get { return this.enablePing; }
            set { this.enablePing = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the rich editor 
        /// is enabled.
        /// </summary>
        public bool EnableRichEditor
        {
            get { return this.enableRichEditor; }
            set { this.enableRichEditor = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool EnableEmailReferral
        {
            get { return this.enableEmailReferral; }
            set { this.enableEmailReferral = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string DefaultEntryStatus
        {
            get { return this.defaultEntryStatus; }
            set { this.defaultEntryStatus = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public short LinkQuantity
        {
            get { return this.linkQuantity; }
            set { this.linkQuantity = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public short FeedQuantity
        {
            get { return this.feedQuantity; }
            set { this.feedQuantity = value; }
        }
        #endregion
        
        #region Methods
        /// <summary>
        /// Saves a Blog.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public AddBlogStatus Save(Guid userID)
        {
            return AddBlogStatus.UnknownError;
        }

        /// <summary>
        /// Creates a Blog.
        /// </summary>
        /// <returns>Returns the status of the Add operation.</returns>
        public AddBlogStatus Save()
        {
            AddBlogStatus status = AddBlogStatus.UnknownError;
            this.id = Guid.NewGuid();
            this.isDeleted = false;
            this.isPrivate = true;
            this.creationDate = DateTime.Now;
            this.creator = Membership.GetUser(System.Web.HttpContext.Current.User.Identity.Name);
            this.lastUpdateDate = DateTime.MinValue;
            status = new BlogRepository().SaveBlog(this);
            return status;
        }

        /// <summary>
        /// Updates the Blog.
        /// </summary>
        /// <returns>Returns the status of the Edit operation.</returns>
        public EditBlogStatus Edit()
        {
            EditBlogStatus status = EditBlogStatus.UnknownError;
            this.lastUpdateDate = DateTime.Now;
            this.updator = Membership.GetUser(System.Web.HttpContext.Current.User.Identity.Name);
            IBlogRepository repository = new BlogRepository();
            status = repository.EditBlog(this);
            return status;
        }

        /// <summary>
        /// Marks the record of Blog as deleted. 
        /// </summary>
        /// <returns>Returns the status of the Delete operation.</returns>
        public static DeleteBlogStatus Delete(Guid id)
        {
            DeleteBlogStatus status = DeleteBlogStatus.UnknownError;
            return status;
        }

        /// <summary>
        /// Permanently remove the record from the database.
        /// </summary>
        /// <returns>Returns the status of the Remove operation.</returns>
        public static RemoveBlogStatus Remove(Guid id)
        {
            RemoveBlogStatus status = RemoveBlogStatus.UnknownError;
            return status;
        }

        /// <summary>
        /// Loads a collection of Blog based on userID.
        /// </summary>
        /// <param name="userID">The unique ID of the blogs.</param>
        /// <returns>AllowHtmlInComment collection of Blog.</returns>
        public static List<Blog> FindAllByUserID(Guid userID)
        {
            List<Blog> blogs = new List<Blog>();
            return blogs;
        }
        /// <summary>
        /// Loads a Blog based on its unique ID.
        /// </summary>
        /// <param name="id">The unique ID of the Blog.</param>
        /// <returns>The Blog.</returns>
        public static Blog Find(System.Guid ID)
        {
            return new BlogRepository().FindBlog(ID);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Find()
        {
            
        }

        /// <summary>
        /// Loads an instance of Blog based on its Name.
        /// </summary>
        /// <param name="name">The unique ID of the Blog.</param>
        /// <returns>The Blog.</returns>
        public static Blog FindByName(string name)
        {
            Blog blog = new Blog();
            return blog;
        }

        /// <summary>
        /// Loads a paginated collection of Blog based on userID.
        /// </summary>
        /// <param name="userID">The unique ID of the user.</param>
        /// <param name="pageIndex">The page index to start retrieving records.</param>
        /// <param name="pageSize">The number of records to retrieve in a given page.</param>
        /// <returns>AllowHtmlInComment paginated collection of Blog.</returns>
        public static List<Blog> FindAllByUserID(Guid userID, int pageIndex, int pageSize, out int count)
        {
            IBlogRepository repository = new BlogRepository();
            return repository.FindAllBlogsByUserID(userID, pageIndex, pageSize, out count);
        }

        /// <summary>
        /// Loads a collection of paginated Entries.
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Entry> FindEntries(int pageIndex, int pageSize, out int count)
        {
            IEntryRepository repository = new EntryRepository();
            return repository.FindEntries(pageIndex, pageSize, out count);
        }

        /// <summary>
        /// Loads a collection of Entries.
        /// </summary>
        /// <returns></returns>
        public List<Entry> FindEntries()
        {
            IEntryRepository repository = new EntryRepository();
            return repository.FindEntries();
        }
        #endregion
    }
}